package com.xlx.ui.service;

import com.xlx.ui.controller.MessageWebsocket;
import com.xlx.ui.domain.DTO.FriendsDTO;
import com.xlx.ui.domain.MO.Friends;
import com.xlx.ui.domain.MO.User;
import com.xlx.ui.mapper.FriendsMapper;
import com.xlx.ui.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: 张川
 * @date: 2021-04-17 19:43
 */
@Service
@Slf4j
public class FriendsService{

    private FriendsMapper friendsMapper;
    private UserMapper userMapper;

    public FriendsService(FriendsMapper friendsMapper, UserMapper userMapper) {
        this.friendsMapper = friendsMapper;
        this.userMapper = userMapper;
    }


    public boolean add(Integer userId, Integer friendsId) {
        Friends friends = new Friends();
        friends.setUserid(userId);
        friends.setFriendsid(friendsId);
        friends.setActive(0);
        MessageWebsocket.clients.forEach((k,v)->{
            if(v.getUserId().equals(String.valueOf(friendsId))){
              friends.setActive(1);
              return;
            }
        });
        int i = friendsMapper.insertSelective(friends);
        return i>0;
    }

    //查询好友
    public List<FriendsDTO> search(Integer userId){
        List<Friends> friends = friendsMapper.selectByExample(Example.builder(Friends.class).where(Sqls.custom().andEqualTo("userid", userId)).build());
        ArrayList<Object> friendsId = new ArrayList<>();
        friends.forEach(friends1 ->friendsId.add( friends1.getFriendsid()));
        List<User> users = userMapper.selectByExample(Example.builder(User.class).where(Sqls.custom().andIn("id",friendsId)).build());
        ArrayList<FriendsDTO> friendsDTOS = new ArrayList<>();
        users.forEach(user -> {
            FriendsDTO friendsDTO = new FriendsDTO();
            friendsDTO.setId(user.getId());
            friendsDTO.setEmail(user.getEmail());
            friendsDTO.setName(user.getName());
            friendsDTO.setPhoto(user.getPhoto());
            Friends friends2 = friends.stream().filter(friends1 -> friends1.getFriendsid().equals(user.getId())).findFirst().orElse(null);
            if(friends2!=null){
                friendsDTO.setComments(friends2.getComments());
                friendsDTO.setActive(friends2.getActive());
            }
            friendsDTOS.add(friendsDTO);
        });
        return friendsDTOS;
    }

    /**
     * 设置备注
     * @param friends
     * @return
     */
    public boolean setbz( Friends friends) {
        Friends friends1 = new Friends();
        friends1.setComments(friends.getComments());
        Example build = Example.builder(Friends.class).where(Sqls.custom().andEqualTo("userid", friends.getUserid()).andEqualTo("friendsid", friends.getFriendsid())).build();
        int i = friendsMapper.updateByExampleSelective(friends1, build);
        return i>0;
    }

    /**
     * 修改好友状态
     */
    public void setActive(Friends friends){
       friendsMapper.updateByExampleSelective(friends,Example.builder(Friends.class).where(Sqls.custom().andEqualTo("friendsid",friends.getFriendsid())).build());
    }


    /**
     * 删除好友
     * @param friends
     * @return
     */
    public Boolean remove(Friends friends) {
        int delete = friendsMapper.delete(friends);
        return true;
    }
}
